<template>
  <div class="articles-container">
    <el-card>
      <!-- 筛选 -->
      <el-row>
        <el-col :span="18">
          <el-form label-width="60px" size="small" :inline="true">
            <el-form-item label="关键字">
              <el-input style="200px" v-model="articlesList.keyword"></el-input>
           </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="articlesList.state">
                <el-option :value="1" label="启用"></el-option>
                <el-option :value="0" label="禁用"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item style="text-align:right">
              <el-button @click="clear()">清除</el-button>
              <el-button @click="search()" type="primary">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6" style="text-align:right">
          <el-button icon="el-icon-edit" @click="addDialog()" type="success" size="small">新增技巧</el-button>
        </el-col>
      </el-row>
      <!-- 数据记录 -->
      <el-alert
        :title="`数据一共 ${total} 条`"
        style="margin-bottom:15px"
        type="info"
        closable
        show-icon
      ></el-alert>
      <!-- 表格 -->
      <el-table :data="articles" v-loading="loading">
        <el-table-column label="序号" type="index" width="80px" :index="indexMethod"></el-table-column>
        <el-table-column label="文章标题" width="400px">
          <template #default={row}>
            {{row.title}}
            <a @click.prevent="Video(row.videoURL)" v-if="row.videoURL" href="#" class="el-icon-video-play"></a>
          </template>
        </el-table-column>
        <el-table-column label="阅读数" prop="visits"></el-table-column>
        <el-table-column label="录入人" prop="username"></el-table-column>
        <el-table-column label="录入时间">
          <template #default={row}>{{row.createTime|fmTime}}</template>
        </el-table-column>
        <el-table-column label="状态" width="100px">
          <template #default={row}>{{row.state===1?'已禁用':'已启用'}}</template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template #default={row}>
            <el-button type="text" @click="previewArticle(row)">预览</el-button>
            <el-button type="text" @click="toggleState(row)">{{row.state===1?'禁用':'启用'}}</el-button>
            <el-button type="text" @click="addDialog(row)" :disabled="row.state===1">修改</el-button>
            <el-button type="text" @click="delArticle(row)" :disabled="row.state===1||row.totals > 0">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="margin-top:20px;text-align:right"
        background
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        :page-size="articlesList.pagesize"
        :current-page="articlesList.page"
        @current-change="pager"
        :page-sizes="[5,10,20,50]"
        @size-change="handleSizeChange"
      ></el-pagination>
    </el-card>
    <!-- 添加|修改 -->
    <articles-add ref="articlesAdd" :data="currArticle" @updateList="updateList()"></articles-add>
    <!-- 预览 -->
    <articles-preview ref="articlesPreview" :data="currArticle"></articles-preview>
    <!-- 视频 -->
    <div class="video-preview" v-if="videoURL">
      <div class="close" @click="closeVideo()"><span class="el-icon-close"></span></div>
      <div class="box">
        <video ref="video" :src="videoURL"></video>
      </div>
    </div>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/articles'
import ArticlesAdd from '../components/articles-add'
import ArticlesPreview from '../components/articles-preview'
export default {
  name: 'ArticlesList',
  components: { ArticlesAdd, ArticlesPreview },
  data () {
    return {
      articlesList: {
        keyword: null, // 搜索
        state: null, // 状态
        page: 1, // 页码
        pagesize: 5 // 条数
      },
      total: 0, // 总数
      articles: [], // 表格的数据
      loading: false, // loading效果
      currArticle: {}, // 添加预览的数据
      videoURL: null // 视屏地址
    }
  },
  created () {
    this.getArticleList()
  },
  methods: {
    // 查询列表
    async getArticleList () {
      this.loading = true
      const { data } = await list(this.articlesList)
      this.articles = data.items
      this.total = data.counts
      this.loading = false
    },
    // 自定义索引列
    indexMethod (index) {
      return index + 1 + this.articlesList.pagesize * (this.articlesList.page - 1)
    },
    // 添加 编辑
    addDialog (article = {}) {
      this.currArticle = article
      this.$nextTick(() => {
        this.$refs.articlesAdd.open()
      })
    },
    // 切换状态
    async toggleState (article) {
      await changeState({
        id: article.id,
        state: article.state === 1 ? 0 : 1
      })
      this.$message.success('操作成功')
      article.state = article.state === 1 ? 0 : 1
    },
    // 删除
    async delArticle (article) {
      const str = await this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        type: 'warning'
      }).catch(() => {})
      // console.log(str)
      if (str === 'confirm') {
        await remove(article)
        this.$message.success('删除成功')
        this.getArticleList()
      }
    },
    // 清除筛选条件
    clear () {
      this.articlesList = {
        keyword: null, // 搜索
        state: null, // 状态
        page: 1, // 页码
        pagesize: 5 // 条数
      }
      this.getArticleList()
    },
    // 进行筛选
    search () {
      this.articlesList.page = 1
      this.getArticleList()
    },

    // 切换条数
    handleSizeChange (size) {
      this.articlesList.page = 1
      this.articlesList.pagesize = size
      this.getArticleList()
    },
    // 切换分页
    pager (v) {
      this.articlesList.page = v
      this.getArticleList()
    },
    // 新增|修改 后更新列表
    updateList () {
      if (!this.currArticle.id) {
        this.articlesList.page = 1
      }
      this.getArticleList()
    },
    // 预览
    previewArticle (article) {
      this.currArticle = article
      this.$nextTick(() => {
        this.$refs.articlesPreview.open()
      })
    },
    // 视频
    Video (url) {
      this.videoURL = url
      this.$nextTick(() => {
        this.$refs.video.play()
      })
    },
    // 关闭视频
    closeVideo () {
      this.videoURL = null
    }
  }
}
</script>

<style lang="scss" scoped>
.articles-container{
  padding: 10px;
  .el-icon-video-play{
    color: blue;
    font-size: 18px;
    vertical-align: middle
  }
}
.video-preview{ // 视频遮罩
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.3);
  overflow: hidden;
  z-index: 99999999999;
  .close{ // 关闭按钮样式
    width: 50px;
    height: 50px;
    position: absolute;
    top: 30px;
    left: 50%;
    transform: translate(-50%,0);
    background: rgba(0,0,0,0.4);
    box-shadow: 0 0 5px rgba(0,0,0,0.4);
    border-radius: 50%;
    text-align: center;
    line-height: 50px;
    color: #fff;
    font-size: 20px;
  }
  .box{ // 播放视频
    width: 800px;
    height: 600px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    video{
      width: 100%;
      height: 100%;
    }
  }
}
</style>
